package com.carkeeper.user.service.impl;


import com.carkeeper.user.dao.CarMapper;
import com.carkeeper.user.dao.UserMapper;
import com.carkeeper.user.pojo.Car;
import com.carkeeper.user.pojo.User;
import com.carkeeper.user.pojo.UserAndCar;
import com.carkeeper.user.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CarMapper carMapper;

    /*
     * 条件查询+分页查询
     * */
    @Override
    public Page<User> findPage(User user, Integer page, Integer size) {
        //设置分页
        PageHelper.startPage(page,size);

        //多条件查询
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        if (user !=null){
            //车主名称查询
            if (user.getNickname() !=null && !"".equals(user.getNickname())){
                criteria.andLike("nickname","%"+user.getNickname()+"%");
            }
            //手机号查询
            if (user.getPhone() !=null && !"".equals(user.getPhone())){
                criteria.andEqualTo("phone",user.getPhone());
            }
            //状态查询
            if (user.getStatus() !=null && !"".equals(user.getStatus())){
                criteria.andEqualTo("status",user.getStatus());
            }
        }
        Page<User> pageUser = (Page<User>) userMapper.selectByExample(example);
        return pageUser;
    }

    /*
     * 锁定车主主体
     * */
    @Override
    public void lock(Long id) {
        if (id == null ){
            throw new RuntimeException("车主不存在");
        }
        User user = userMapper.selectByPrimaryKey(id);
        if ("0".equals(user.getStatus())){
            //设置状态为1 解锁
            user.setStatus("1");
        }
        if ("1".equals(user.getStatus())){
            //设置状态为0 锁定
            user.setStatus("0");
        }
        userMapper.updateByPrimaryKeySelective(user);
    }

    /*
     *  查询user 和 car的所有数据
     * */
    @Override
    public List<UserAndCar> findAll() {
        List<User> users = userMapper.selectAll();
        if (users == null){
            throw new RuntimeException("无数据");
        }
        List<UserAndCar> list = new ArrayList<>();
        Integer count = 1;
        for (User user : users) {
            Car car = new Car();
            car.setUserId(user.getId());
            //根据车主id查询车主车辆信息集合
            List<Car> carList = carMapper.select(car);
            for (Car car1 : carList) {
                UserAndCar userAndCar = new UserAndCar();       //报表实体类
                userAndCar.setId(count++);
                userAndCar.setNickname(user.getNickname());     //车主名称
                userAndCar.setPhone(user.getPhone());           //手机号
                if("0".equals(user.getStatus())){
                    user.setStatus("锁定");
                }
                if("1".equals(user.getStatus())){
                    user.setStatus("正常");
                }
                userAndCar.setStatus(user.getStatus());         //状态

                //以下是车辆表的信息
                userAndCar.setName(car1.getName());             //品牌
                userAndCar.setSeries(car1.getSeries());         //车系
                userAndCar.setMode(car1.getMode());             //车型
                userAndCar.setCode(car1.getCode());             //车牌
                userAndCar.setFrameCode(car1.getFrameCode());   //车架号
                userAndCar.setEngineCode(car1.getEngineCode()); //发动机号
                list.add(userAndCar);
            }
        }
        return list;
    }


    /**
     * 根据车主id查询车主信息
     * @param id
     * @return
     */
    @Override
    public User findById(Long id) {
        if (id == null){
            throw new RuntimeException("无数据");
        }
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }
}
